package org.jetnuke.modules.admin.stories;

import org.apache.log4j.Logger;

import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.Queue;

public class Show implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(Show.class);

	int qid;

	public void setQid(int qid) {
		this.qid = qid;
	}

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		if (ai == null || (!ai.isSuperUser()))
			return null;// Error;
		Queue story = DataAccessDriver.getInst().newQueueDAO()
				.retrieveByPK(qid);
		vc.put("queue", story);
		GregorianCalendar gc = new GregorianCalendar();
		vc.put("tday", new Integer(gc.get(Calendar.DATE)));
		vc.put("tmonth", new Integer(gc.get(Calendar.MONTH) + 1));
		vc.put("tyear", new Integer(gc.get(Calendar.YEAR)));
		vc.put("categories", DataAccessDriver.getInst().newStoriesCatDAO()
				.selectAll());

		// stories_catPeer.doSelect(new Criteria()
		// .addAscendingOrderByColumn(stories_catPeer.TITLE)));
		vc.put("topics", DataAccessDriver.getInst().newTopicsDAO()
				.selectAllOrderTopicName());// topicsPeer.doSelect(new
											// Criteria()
		// .addAscendingOrderByColumn(topicsPeer.TOPICTEXT)));
		vc.put("enc_story", story.getSTORY());

		// TextUtilities.replaceTags(TextUtilities
		// .replaceAmp(story.getSTORY())));
		vc.put("enc_story_ext", story.getSTORYEXT());// //TextUtilities.replaceTags(TextUtilities
		// .replaceAmp(story.getSTORYEXT())));
		ActionForward returnActionForward = new ActionForward(op, info);
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return returnActionForward;
	}
}
